<!DOCTYPE html>
<html lang="en">
<head>
        <meta charset="utf-8" />
        <title>PythonTutorials - python</title>
        <link rel="stylesheet" href="/theme/css/main.css" />

        <!--[if IE]>
            <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
        <![endif]-->
</head>

<body id="index" class="home">
        <header id="banner" class="body">
                <h1><a href="/">PythonTutorials </a></h1>
                <nav><ul>
                    <li><a href="/category/cpp.html">cpp</a></li>
                    <li class="active"><a href="/category/python.html">python</a></li>
                </ul>
                </nav>
        </header><!-- /#banner -->

                <section id="content" class="body">
                    <ol id="posts-list" class="hfeed" start="2">
            <li><article class="hentry">
                <header>
                    <h1><a href="/Part.2.D.1-args.html" rel="bookmark"
                           title="Permalink to Part.2.D.1-args">Part.2.D.1-args</a></h1>
                </header>

                <div class="entry-content">
<footer class="post-info">
        <span>Mon 06 April 2020</span>
<span>| tags: <a href="/tag/python.html">python</a><a href="/tag/progarmming.html">progarmming</a><a href="/tag/excel.html">Excel</a></span>
</footer><!-- /.post-info -->                <h1>关于参数（上）</h1>
<p>之前就提到过，从结构上来看，每个函数都是一个完整的程序，因为一个程序，核心构成部分就是输入、处理、输出：</p>
<blockquote>
<ul>
<li>它可以有<strong>输入</strong> —— 即，它能接收外部通过参数传递的值；</li>
<li>它可以有<strong>处理</strong> —— 即，内部有能够完成某一特定任务的代码；尤其是，它可以根据 “输入” 得到 “输出”；</li>
<li>它可以有<strong>输出</strong> —— 即，它能向外部输送返回值……</li>
</ul>
</blockquote>
<p>所以，在我看来，有了一点基础知识之后，最早应该学习的是 “如何写函数” —— 这个起点会更好一些。</p>
<p>这一章的内容，看起来会感觉与 <a href="Part.1.E.4.functions.md">Part1.E.4 函数那一章</a>部分重合。但这两章的出发点不一样：</p>
<blockquote>
<ul>
<li><a href="Part.1.E.4.functions.md">Part1.E.4 函数那一章</a>，只是为了让读者有 “阅读” 函数说明文档的能力；</li>
<li>这一章，是为了让读者能够开始动手写函数给自己或别人用……</li>
</ul>
</blockquote>
<h2>为函数取名 …</h2>
                <a class="readmore" href="/Part.2.D.1-args.html">read more</a>
                </div><!-- /.entry-content -->
            </article></li>

            <li><article class="hentry">
                <header>
                    <h1><a href="/Part.2.D.2-aargs.html" rel="bookmark"
                           title="Permalink to Part.2.D.2-aargs">Part.2.D.2-aargs</a></h1>
                </header>

                <div class="entry-content">
<footer class="post-info">
        <span>Mon 06 April 2020</span>
<span>| tags: <a href="/tag/python.html">python</a><a href="/tag/progarmming.html">progarmming</a><a href="/tag/excel.html">Excel</a></span>
</footer><!-- /.post-info -->                <h1>关于参数（下）</h1>
<h2>可以接收一系列值的位置参数</h2>
<p>如果你在定义参数的时候，在一个<em>位置参数</em>（Positional Arguments）前面标注了星号，<code>*</code>，那么，这个位置参数可以接收一系列值，在函数内部可以对这一系列值用 <code>for ... in ...</code> 循环进行逐一的处理。</p>
<p>带一个星号的参数，英文名称是 “Arbitrary Positional Arguments”，姑且翻译为 “随意的位置参数”。</p>
<p>还有带两个星号的参数，一会儿会讲到，英文名称是 “Arbitrary Keyword Arguments”，姑且翻译为 “随意的关键字参数”。</p>
<blockquote>
<p>有些中文书籍把 “Arbitrary Positional Arguments” 翻译成 “可变位置参数”。事实上，在这样的地方，无论怎样的中文翻译都是令人觉得非常吃力的。前面的这个翻译还好了，我还见过把 “Arbitrary Keyword Arguments” 翻译成 “武断的关键字参数” 的 —— 我觉得这样的翻译肯定会使读者产生说不明道不白的疑惑。</p>
<p>所以，<strong>入门之后就尽量只用英文 …</strong></p></blockquote>
                <a class="readmore" href="/Part.2.D.2-aargs.html">read more</a>
                </div><!-- /.entry-content -->
            </article></li>

            <li><article class="hentry">
                <header>
                    <h1><a href="/Part.2.D.3-lambda.html" rel="bookmark"
                           title="Permalink to Part.2.D.3-lambda">Part.2.D.3-lambda</a></h1>
                </header>

                <div class="entry-content">
<footer class="post-info">
        <span>Mon 06 April 2020</span>
<span>| tags: <a href="/tag/python.html">python</a><a href="/tag/progarmming.html">progarmming</a><a href="/tag/excel.html">Excel</a></span>
</footer><!-- /.post-info -->                <h1>化名与匿名</h1>
<h2>化名</h2>
<p>在 Python 中，我们可以给一个函数取个<strong>化名</strong>（alias）。</p>
<p>以下的代码，我们先是定义了一个名为 <code>_is_leap</code> 的函数，而后为它另取了一个化名：</p>
<div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">IPython.core.interactiveshell</span> <span class="kn">import</span> <span class="n">InteractiveShell</span>
<span class="n">InteractiveShell</span><span class="o">.</span><span class="n">ast_node_interactivity</span> <span class="o">=</span> <span class="s2">&quot;all&quot;</span>

<span class="k">def</span> <span class="nf">_is_leap</span><span class="p">(</span><span class="n">year</span><span class="p">):</span>
    <span class="k">return</span> <span class="n">year</span> <span class="o">%</span> <span class="mi">4</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">and</span> <span class="p">(</span><span class="n">year</span> <span class="o">%</span> <span class="mi">100</span> <span class="o">!=</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">year</span> <span class="o">%</span> <span class="mi">400</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span>

<span class="n">year_leap_bool</span> <span class="o">=</span> <span class="n">_is_leap</span>
<span class="n">year_leap_bool</span>              <span class="c1">#&lt;function __main__._is_leap(year)&gt;</span>
<span class="n">year_leap_bool</span><span class="p">(</span><span class="mi">800</span><span class="p">)</span>         <span class="c1"># _is_leap(800) -&gt; True</span>

<span class="nb">id …</span></pre></div>
                <a class="readmore" href="/Part.2.D.3-lambda.html">read more</a>
                </div><!-- /.entry-content -->
            </article></li>
            </ol><!-- /#posts-list -->
<p class="paginator">
        <a href="/category/python6.html">&laquo;</a>
    Page 7 / 17
        <a href="/category/python8.html">&raquo;</a>
</p>
            </section><!-- /#content -->
        <section id="extras" class="body">
                <div class="blogroll">
                        <h2>blogroll</h2>
                        <ul>
                            <li><a href="http://getpelican.com/">Pelican</a></li>
                            <li><a href="http://python.org/">Python.org</a></li>
                            <li><a href="http://jinja.pocoo.org/">Jinja2</a></li>
                            <li><a href="#">You can modify those links in your config file</a></li>
                        </ul>
                </div><!-- /.blogroll -->
                <div class="social">
                        <h2>social</h2>
                        <ul>

                            <li><a href="#">You can add links in your config file</a></li>
                            <li><a href="#">Another social link</a></li>
                        </ul>
                </div><!-- /.social -->
        </section><!-- /#extras -->

        <footer id="contentinfo" class="body">
                <p>Powered by <a href="http://www.maxiaoan.com/">Maxiaoan</a></p>
        </footer><!-- /#contentinfo -->

</body>
</html>